********************************************************************
* Constitution Data- Long
* Jonathan Slapin, October 14, 2005
* Infiles: european convention-final, uppsalaJBS
* reshapes data long
* examines indep vars: institutional positions, dist to sq, population, 
* ratification restraints, deviation among memstate reps.
* Missingness corrected by assuming indifference
************************************************************************

clear
version 8
set mem 50m
set more off
set matsize 150

* set directory and load data
* merge the dataset which contains saliency with the main data which contains country codes
cd "/Users/jonathan/Dropbox (Personal)/TKoenigRepData/KoenigSlapinWorldPolitics"
  
use "european convention - final.dta", clear
sort name
save "tempfiles/temp1.dta", replace   

use "uppsalaJBS.dta", clear   
sort name
merge name using "tempfiles/temp1.dta"
drop _merge

* rename variables to prep the data to make it long

rename q1a_out   outq1
rename q1a_sq    sqq1
rename q1a          resq1

rename q1a1_out outq2_1
rename q1a1_sq  sqq2_1
rename q2a_1      resq2_1

rename q1a2_out  outq2_2
rename q1a2_sq   sqq2_2
rename q2a_2      resq2_2

rename q1a3_out outq2_3
rename q1a3_sq  sqq2_3
rename q2a_3      resq2_3

rename q1a4_out  outq2_4
rename q1a4_sq   sqq2_4
rename q2a_4       resq2_4

rename q1a5_out   outq2_5
rename q1a5_sq    sqq2_5 
rename q2a_5        resq2_5

* drop question 3 because outcome not labeled
drop q3a_sq 
drop q3a_1 
drop q3a_2 
drop q3a_3 
drop q3a_4 
drop q3a_5 
drop q3a_6 
drop q3a_7 
drop q3a_8 
drop q3a_9 
drop q3a_10 
drop q3a_11 
drop q3a_12 
drop q3a_13 
drop q3a_14 
drop q3a_15 
drop q3a_16 
drop q3a_17 
drop q3a_18 
drop q3a_19 
drop q3a_20 
drop q3a_21 
drop q3a_22 
drop q3a_23 
drop q3a_24 
drop q3a_25

rename q4a_out outq4
rename q4a_sq  sqq4
rename q4a        resq4

rename q5a_out outq5
rename q5a_sq  sqq5
rename q5a        resq5

rename q6a_out outq6
rename q6a_sq  sqq6
rename q6a        resq6

rename q7a_out outq7 
rename q7a_sq  sqq7
rename q7a        resq7

rename q8a_out  outq8
rename q8a_sq   sqq8
rename q8a         resq8

rename q9a_out   outq9
rename q9a_sq     sqq9
rename q9a          resq9

rename q2a_2_ou  outq9_2
rename q2a_2_sq  sqq9_2
rename q9a_2        resq9_2

rename q10a_out   outq10
rename q10a_sq    sqq10
rename q10a          resq10

rename q11a_out    outq11
rename q11a_sq     sqq11
rename q11a           resq11

rename q12a_out     outq12
rename q12a_sq      sqq12
rename q12a            resq12

rename q13a_out    outq13
rename q13a_sq     sqq13
rename q13a          resq13

rename q14a  resq14

rename q15a_out   outq15
rename q15a_sq    sqq15
rename q15a          resq15

rename q16a_out   outq16
rename q16a_sq    sqq16
rename q16a          resq16

rename q17a_out   outq17
rename q17a_sq    sqq17
rename q17a          resq17

rename q18a_out   outq18
rename q18a_sq    sqq18
rename q18a          resq18

rename q19a_out   outq19
rename q19a_sq    sqq19
rename q19a          resq19

rename q19_2_ou   outq19_2
rename q19_2_sq   sqq19_2
rename q19a_2       resq19_2

rename q20a_out   outq20
rename q20a_sq    sqq20
rename q20a          resq20

rename q21a_out   outq21
rename q21a_sq    sqq21
rename q21a          resq21

rename q22a_out   outq22
rename q22a_sq    sqq22
rename q22a          resq22

rename q23a         resq23

rename zq1b zq1
rename zq2b zq2
rename zq4b zq4
rename zq5b zq5
rename zq6b zq6
rename zq7b zq7
rename zq8b zq8
rename zq9b zq9
rename zq10b zq10
rename zq11b zq11
rename zq12b zq12
rename zq13b zq13
rename zq14b zq14
rename zq15b zq15
rename zq16b zq16
rename zq17b zq17
rename zq18b zq18
rename zq19b zq19
rename zq20b zq20
rename zq21b zq21
rename zq22b zq22
rename zq23b zq23

rename q1b      q1
rename q2b      q2

drop q3_1b  
drop q3_2b  
drop q3_3b 
drop q3_4b 
drop q3_5b 
drop q3_6b 
drop q3_7b 
drop q3_8b 
drop q3_9b 
drop q3_10b 
drop q3_11b 
drop q3_12b 
drop q3_13b 
drop q3_14b 
drop q3_15b 
drop q3_16b  
drop q3_17b 
drop q3_18b 
drop q3_19b 
drop q3_20b 
drop q3_21b 
drop q3_22b
drop q3_23b 
drop q3_24b 
drop q3_25b 

rename q4b  q4
rename q5b  q5
rename q6b  q6
rename q7b  q7
rename q8b  q8
rename q9b  q9
rename q10b q10
rename q11b q11
rename q12b q12
rename q13b q13
rename q14b q14
rename q15b q15
rename q16b q16
rename q17b q17
rename q18b q18
rename q19b q19
rename q20b q20
rename q21b q21
rename q22b q22
rename q23b q23

* change prefix for saliency variables
renpfix  q saliencyq

* fix saliencies for questions 2, 9 and 19. Apply saliencies for the 1st part of the question to all parts

gen saliencyq2_1=saliencyq2
gen saliencyq2_2=saliencyq2
gen saliencyq2_3=saliencyq2
gen saliencyq2_4=saliencyq2
gen saliencyq2_5=saliencyq2

gen zq2_1=zq2
gen zq2_2=zq2
gen zq2_3=zq2
gen zq2_4=zq2
gen zq2_5=zq2

drop saliencyq2
drop zq2

gen saliencyq9_2=saliencyq9
gen zq9_2=zq9

gen saliencyq19_2=saliencyq19
gen zq19_2=zq19

* create unique numerical id
gen id=_n
save "tempfiles/temp2.dta", replace

* reshape data long 
reshape long out sq res saliency z, i(id) j(question) string

* give important variables better names
rename z zsaliency
rename res response
rename out outcome
save "tempfiles/temp3.dta", replace

* correct missingness by assuming indifference
replace response=(abs(outcome-sq))/2 if response==.

* drop q14 and q23 because sq or outcome is missing
drop if question=="q14"| question=="q23"

* create win and lose variables: 1 if negotiator realizes his or her preference
gen win=1 if outcome==response
replace win=0 if outcome~=response

* create alternate dep var: distance to outcome
gen distout=abs(outcome-response)

* Gen dep vars accounting for saliency.  Use the z-score saliency
* Correct missing saliency by assuming missingness equals the average.
replace zsaliency=0 if zsaliency==.

* Add lowest zscore to make all weights positive. Necessary because otherwise winning on something 
* that a delegate cares less than average about would be counted as worse than losing. 
bys id: egen minimumsal=min(zsaliency)
gen poszsaliency=abs(minimumsal)+zsaliency
gen winsal=win*poszsaliency
gen distoutsal=distout*poszsaliency

* create number of wins, number of wins saliency, distout, distoutsal
bys id: egen numwin=sum(win)
bys id: egen numwinsal=sum(winsal)
bys id: egen totdistout=sum(distout)
bys id: egen totdistoutsal=sum(distoutsal)

* count wins for actors on the sq
bys id: egen numwinsq=sum(win) if (response==sq & sq~=.)

* create dummy variables for negotiator position- presidency, mem state gov, natparl, EP, Comm ect..
* head of state or mem state government is left out category
xi i.position
rename _Iposition_3 natparl
rename _Iposition_4 ep
rename _Iposition_5 com
rename _Iposition_6 accgov
rename _Iposition_7 accnatparl

*Create EUpopulation variable- variable coded from Eurostat population data release August 7th 2002
*population in millions;  I then generate lnpop- the log of the EUpopulation variable

gen EUpopulation= 8.12 if country==1
replace EUpopulation= 8.15 if country==2
replace EUpopulation= 10.26 if country==3
replace EUpopulation=  .76     if country==4
replace EUpopulation= 10.3  if country==5
replace EUpopulation= 5.35 if country==6
replace EUpopulation= 1.38 if country==7
replace EUpopulation= 5.18 if country==8
replace EUpopulation=59.04 if country==9
replace EUpopulation= 82.26 if country==10
replace EUpopulation= 10.56 if country==11
replace EUpopulation= 10.20 if country==12
replace EUpopulation= 3.83 if country==13
replace EUpopulation= 57.84 if country==14
replace EUpopulation= 2.37 if country==15
replace EUpopulation= 3.49 if country==16
replace EUpopulation= .44 if country==17
replace EUpopulation= 15.99 if country==18
replace EUpopulation= 38.64 if country==19
replace EUpopulation= 10.26 if country==20
replace EUpopulation= 22.43 if country==21
replace EUpopulation= 5.38 if country==22
replace EUpopulation= 1.89 if country==23
replace EUpopulation= 40.12 if country==24
replace EUpopulation= 8.88 if country==25
replace EUpopulation= 68.04 if country==26
replace EUpopulation= 59.89 if country==27
replace EUpopulation= .39 if country==28

gen lnpop= log(EUpopulation)

* Here I generate a variable to test the effect of variance among respondents from the same country. 
* Higher variance should mean fewer wins

bys country question: egen standdev=sd(response)

* generate country range by question
bys country question: egen minresp=min(response)
bys country question: egen maxresp=max(response)
bys country question: gen range=maxresp-minresp

* generate median response. assume median is the agenda setter following Tsebelis
* generate absolute distance to the median voter
bys question: egen median=median(response)
gen distmedian=abs(median-response)

* gen distance to the status quo
gen distsq=abs(sq-response)

* create number of actors supporting sq and change by question
gen supportsq=1 if response==sq
replace supportsq=0 if response~=sq
bys question: egen numsq=sum(supportsq)

gen supportchange=1 if response~=sq
replace supportchange=0 if response==sq
bys question: egen numchange=sum(supportchange)

gen ratiosqchange=numsq/numchange

* merge in new pivot positions from EB data 
sort country question
joinby country question using temppivots, unmatched(both)
* make pivotpos go from 0 to 1
replace pivotpos= pivotpos-1

* run some regs 
* log the dep distoutsal to run some different models
*replace distoutsal=.01 if distoutsal==0
*gen logdistoutsal=ln(distoutsal)

*xi: reg logdistoutsal distmedian distsq lnpop natparl ep com accgov accnatparl i.question 

*xi: reg logdistoutsal distmedian distsq lnpop standdev natparl ep com accgov accnatparl i.question 

*xi: reg logdistoutsal distmedian distsq range natparl ep com accgov accnatparl i.question i.country

* drop observations where someone is to the left of the sq
*drop if response<sq

* create new binomial dep var where "win" refers to change/no change
gen winchange=1 if (response==outcome & outcome==sq ) | (response~=sq & outcome~=sq)
replace winchange=0 if winchange==.

* run regs with robust standard errors

probit win distmedian distsq lnpop ratiosqchange zsaliency natparl ep com accgov accnatparl pivotpos range, robust
probit winchange distmedian distsq lnpop ratiosqchange zsaliency natparl ep com accgov accnatparl pivotpos range, robust

* run regs with pcse 
egen quesnum=group(question)
tsset quesnum id
xtpcse distout distmedian distsq lnpop ratiosqchange zsaliency natparl ep com accgov accnatparl pivotpos range
reg distout distmedian distsq lnpop ratiosqchange zsaliency natparl ep com accgov accnatparl pivotpos range, robust

avplot distmedian,  ytitle("Distance to outcome") xtitle("Distance to median") scheme(s1manual)
graph save median, replace
avplot distsq, ytitle("Distance to outcome") xtitle("Distance to status quo") scheme(s1manual)
graph save distsq, replace
avplot pivotpos, ytitle("Distance to outcome") xtitle("Ratification constraint") scheme(s1manual)
graph save pivotpos, replace
avplot lnpop,  ytitle("Distance to outcome") xtitle("Log population") scheme(s1manual)
graph save lnpop, replace
graph combine median.gph distsq.gph pivotpos.gph lnpop.gph, title("Partial Regression Plots for Key Variables") scheme(s1manual)

hist distout, width(0.2) percent xtitle("Distance to outcome") scheme(s1manual)
graph save histout, replace
hist distsq, width(0.2) percent xtitle("Distance to status quo") scheme(s1manual)
graph save histsq, replace
hist distmedian, width(0.2) percent xtitle("Distance to median") scheme(s1manual)
graph save histmedian, replace

graph combine histout.gph histsq.gph histmedian.gph, holes(4) scheme(s1manual)
graph export combinehist.pdf, replace
****************
* Examine how often the status quo is changed
collapse outcome sq, by(quesnum)
